package com.market.controller.admin;

import com.market.bean.MarketCategory;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminCategoryLabelVo;
import com.market.exception.ValidationException;
import com.market.service.admin.AdminCategoryService;
import com.market.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author:WalterChan
 * @Decription:AdminCategory
 * @Date Created in 2022-12-30-14:45
 */
@RestController
@RequestMapping("admin/category")

public class AdminCategoryController {

    @Autowired
    AdminCategoryService adminCategoryService;

    /**
     *显示一级目录
     */

    @RequestMapping("l1")
    public BaseRespVo listLable(){

        CommonData<AdminCategoryLabelVo> list = adminCategoryService.listLable();
        return BaseRespVo.ok(list);
    }

    @RequestMapping("list")
    public BaseRespVo list()
    {

        CommonData<MarketCategory> list =adminCategoryService.list();
        return BaseRespVo.ok(list);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketCategory marketCategory){
        MarketCategory data= adminCategoryService.create(marketCategory);
        return BaseRespVo.ok(data);
    }
    //    选择成为二级目录必须要有父类目，{errno: 401, errmsg: "参数不对"}
//    {errno: 0, errmsg: "成功"}
//    http://182.92.235.201:8083/admin/category/update
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody @Valid MarketCategory marketCategory, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        BaseRespVo msg = ValidationUtil.getBaseRespVo(bindingResult);
        if (msg!=null) return msg;
        MarketCategory data =adminCategoryService.update(marketCategory);

        return BaseRespVo.ok(data);
    }
//    http://182.92.235.201:8083/admin/category/delete
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCategory marketCategory){
        adminCategoryService.delete(marketCategory);
        return BaseRespVo.ok();

    }


}
